import axios from 'axios'
// axios.defaults.baseURL = 'http://localhost:3001/'
axios.defaults.withCredentials = true
import { Message, Loading } from 'element-ui';

axios.interceptors.request.use(config => {
    console.log(config)
    config.headers.token = window.sessionStorage.getItem('token')
    // 必须return
    return config
})

const axiosContext = axios.create({
    timeout: 20000,
    baseURL: '',
    headers: {},
})
export function serviceRequest(params, autoHandleError = true, showLoading = false) {

    // 处理参数
    let dealParams;
    if ('string' === typeof params) {
        dealParams = {
            url: params,
        };
    } else {
        dealParams = {
            ...params,
        };
    }
    if(!dealParams.headers) dealParams.headers = {};
    // dealParams.headers['Cache-Control'] = 'max-age=0';
    const token = sessionStorage.getItem('token');
    dealParams.headers['token'] = token;

    // 默认为get请求
    dealParams.method = dealParams.method || 'get';

    // 如果是get，那么将参数data变成params;
    if ('get' === dealParams.method) {
        dealParams.params = dealParams.data;
    }
    let loadingCtx;
    if (showLoading) {
        loadingCtx = Loading.service({ fullscreen: true });
    }

    return axiosContext.request(dealParams).then((res) => {
        if (showLoading) loadingCtx.close();
        if (0 === res.data.status) {
            return Promise.resolve(res.data);
        } else {
            return Promise.reject(res.data);
        }
    }).catch((err)=> {
        if (loadingCtx) loadingCtx.close();
        if (autoHandleError) {
            if (!!err && !!(err.message || err.content)) {
                Message({
                    showClose: true,
                    message: err.message || err.content,
                    type: 'error',
                });
            } else {
                Message({
                    showClose: true,
                    message: '网络错误,请稍后重试!',
                    type: 'error',
                });
            }

        }
        // 如果不自动处理错误，那么需要手动catch
        return Promise.reject(err);
    });
}



// 注册提交
export async function goRegist(data){
    return await axios.post('reg',data)
}

// 登陆提交
export async function goLogin(data){
    return await axios.post('login',data)
}

// 获取个人信息
export async function getUserinfo(){
    return await axios.get('user/get')
}
export async function editmyinfo(data){
    return await axios.post('user/edit',data)
}

// 获取商品列表
export async function getoldPhone(){
    return await axios.get('goods/get')
}

// 搜索功能
export async function getSearch(query){
    return await axios.get('goods/get',query)
}
// 回复消息
export async function reparnew(data){
    return await axios.post('user/message/reply',data)
}
// 上传商品
export async function setGoodsfl(){
    return await axios.get('category/get')
}

// 旧去新来根据学校推送 
export async function setGoodsBySc(data){
    return await axios.post('/getGoodsBysc',data)
}

// 加入购物车
// export async function addGoods(data){
//     return await axios.post('/addCart',data)
// }

// 查看所有订单
export async function seeOrderItem(data){
    return await axios.post('/seeOrder',data)
}

// 得到最终可交易的订单列表
export async function getfinorderList(data){
    return await axios.post('/searchOrder',data)
}

// 根据id获取物流详情
export async function getorderByid(data){
    return await axios.post('/searchOrderByid',data)
}

// 点击确定收货
export async function compleorbyid(data){
    return await axios.post('/completeOrderByid',data)
}

// 查看完成订单 
export async function lookcompleorder(data){
    return await axios.post('/seecompleteOrder',data)
}
// 删除完成订单
export async function delcomorderbyid(data){
    return await axios.post('/delcompleteOrder',data)
}

export async function getPosts(data){
    return await axios.get('/user/post/getbyuser', data)
}

export async function addPosts(data){
    return await axios.post('/user/post/add', data)
}
export async function editPosts(data){
    return await axios.post('/user/post/edit', data)
}
export async function delPosts(data){
    return await axios.post('/user/post/del', data)
}

export async function orderGet(data){
    return await axios.get('/user/order/get', {params: data})
}

// 获取产品
export function getProduct(params) {
    return serviceRequest(params);
}
export function addProduct(params) {
    return serviceRequest(params);
}
export function delProduct(params) {
    return serviceRequest(params);
}
export function getCategory(params) {
    return serviceRequest(params);
}